package com.kepler.social.domain.user.account;

import com.kepler.social.common.rule.SmsRule;
import com.kepler.social.common.utils.DateTimeUtil;
import com.kepler.social.common.utils.NickNameUtil;
import com.kepler.social.user.dao.domain.UserInfo;
import com.kepler.social.user.dao.domain.UserSocialInfo;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.Objects;

public class UserAccountFactory {

    public static UserAccountAggregate createOfEmail(String email, Long userId) {
        assert StringUtils.isNoneBlank(email);
        return create(email, null, null, userId);
    }

    public static UserAccountAggregate createOfMobilePhone(Integer countryCallingCode, Long mobilePhoneNumber, Long userId) {
        assert SmsRule.isCccValid(countryCallingCode);
        assert Objects.nonNull(mobilePhoneNumber);
        assert Objects.nonNull(userId);
        return create(null, countryCallingCode, mobilePhoneNumber, userId);
    }

    private static UserAccountAggregate create(String email, Integer countryCallingCode, Long mobilePhoneNumber, Long userId) {
        long nowTime = DateTimeUtil.toMilli(LocalDateTime.now());
        UserInfo userInfo = UserInfo.builder()
                .userId(userId)
                .email(email)
                .countryCallingCode(countryCallingCode).mobilePhoneNumber(mobilePhoneNumber)
                .createTime(nowTime).modifiedTime(nowTime)
                .build();
        UserSocialInfo userSocialInfo = UserSocialInfo.builder()
                .userId(userId)
                .nickName(NickNameUtil.getName())
                .createTime(nowTime).modifiedTime(nowTime)
                .build();
        return new UserAccountAggregate(userInfo, userSocialInfo);
    }


}
